package com.wh.service.outstorehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.api.Api;
import com.wh.mapper.outstorehouse.OutStorehouseMapper;
import com.wh.mapper.outstorehouse.PickingMapper;
import com.wh.pojo.OutStorehouse;
import com.wh.pojo.Picking;
import com.wh.vo.PageResult;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PickingServiceImpl implements PickingService{

    @Autowired
    private PickingMapper pickingMapper;

    @Autowired
    private OutStorehouseMapper outStorehouseMapper;

    @Override
    public List<String> getOptions() {
        QueryWrapper<Picking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","待拣货");
        List<Picking> pickingList = pickingMapper.selectList(queryWrapper);

        List<String> outStorehouseIdList = new ArrayList<>();

        for (Picking picking : pickingList){
            outStorehouseIdList.add(picking.getPickingId());
        }
        return outStorehouseIdList;
    }

    @Override
    public void picking(String id , String user) {
        Picking picking = pickingMapper.selectById(id);
        OutStorehouse outStorehouse = outStorehouseMapper.selectById(picking.getOutStorehouseId());
        //更新拣货单
        picking.setStatus("拣货中")
                .setRecipientPerson(user)
                .setRecipientTime(new Api().getTime());
        pickingMapper.updateById(picking);

        //更新出库单
        outStorehouse.setStatus("拣货中");
        outStorehouseMapper.updateById(outStorehouse);

    }

    @Override
    public PageResult getPickingList(PageResult pageResult) {
        Page<Picking> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());

        boolean flag = StringUtils.hasLength(pageResult.getQuery());

        QueryWrapper<Picking> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag,"pickingId",pageResult.getQuery());
        page = pickingMapper.selectPage(page,queryWrapper);

        pageResult.setRows(page.getRecords()).setTotal((long) page.getRecords().size());
        return pageResult;
    }

    @Override
    public void finish(Picking picking) {
        pickingMapper.updateById(picking);
        OutStorehouse outStorehouse = outStorehouseMapper.selectById(picking.getOutStorehouseId());
        outStorehouse.setStatus("已拣货");
        outStorehouseMapper.updateById(outStorehouse);
    }


}
